package org.bedracket.pgeg.init;

import com.mojang.brigadier.arguments.FloatArgumentType;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.bedracket.pgeg.PGEGMod;

@Mod.EventBusSubscriber
public class ModCommands {

    @SubscribeEvent
    public static void onServerStaring(RegisterCommandsEvent event) {
        var dispatcher = event.getDispatcher();
        dispatcher.register(Commands.literal(PGEGMod.MOD_ID).then(
                Commands.literal("addMana").then(
                        Commands.argument("mana", FloatArgumentType.floatArg())
                                .requires(source -> source.hasPermission(2))
                                .executes(c -> {
                                    CommandSourceStack source = c.getSource();
                                    ServerPlayer player = source.getPlayerOrException();
                                    float san = c.getArgument("mana", Float.class);
                                    player.getCapability(ModCapabilities.PLAYER_MANA_LEVEL).ifPresent(
                                            capability -> {
                                                capability.regenManaLevel(san);
                                            }
                                    );
                                    c.getSource().sendSuccess(
                                            Component.translatable("设置成功，当前为: %.1f".formatted(san)),
                                            false);
                                    return 1;
                                })
                )
        ));
        dispatcher.register(Commands.literal(PGEGMod.MOD_ID).then(
                Commands.literal("reduceMana").then(
                        Commands.argument("mana", FloatArgumentType.floatArg())
                                .requires(source -> source.hasPermission(2))
                                .executes(c -> {
                                    CommandSourceStack source = c.getSource();
                                    ServerPlayer player = source.getPlayerOrException();
                                    float san = c.getArgument("mana", Float.class);
                                    player.getCapability(ModCapabilities.PLAYER_MANA_LEVEL).ifPresent(
                                            capability -> {
                                                capability.reduceManaLevel(san);
                                            }
                                    );
                                    c.getSource().sendSuccess(
                                            Component.translatable("设置成功，当前为: %.1f".formatted(san)),
                                            false);
                                    return 1;
                                })
                )
        ));
    }
}
